package com.lagou.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {
    private ChannelHandlerContext context;
    private String result;
    private String para;
    private Object[] params;

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        context = ctx;
    }

    /**
     * 收到服务器数据，唤醒等待线程
     * @param ctx
     * @param msg
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg){
        result = msg.toString();
        notify();
    }


    public synchronized Object call() throws InterruptedException{
//        context.writeAndFlush(para);
        context.writeAndFlush(params[0]);
        wait();
        return result;
    }

    public String getPara() {
        return para;
    }

    public void setPara(String para) {
        this.para = para;
    }

    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }
}
